package com.zmn.oms.admin.controller.work.modify;

import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.oms.admin.controller.work.WorkBaseController;
import com.zmn.oms.business.interfaces.work.OrderWorkListBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.model.dto.work.compensate.CompensateDTO;
import com.zmn.oms.model.dto.work.compensate.CompensateReviewDTO;
import com.zmn.oms.model.dto.work.compensate.RemitVoucherDTO;
import com.zmn.oms.model.dto.work.refund.VoucherDataDTO;
import com.zmn.oms.model.entity.pay.OrderPay;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.entity.work.OrderWorkQuery;
import com.zmn.oms.model.vo.work.OperatePermissionVO;
import com.zmn.oms.model.vo.work.compensate.CompensateVO;
import com.zmn.oms.model.vo.work.compensate.RemitVoucherVO;
import com.zmn.oms.model.vo.work.refund.RefundBankVO;
import com.zmn.oms.services.interfaces.pay.OrderPayService;
import com.zmn.oms.zmn.business.interfaces.compensate.CompensateBService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkOperatePermissionBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName WorkCompensateController
 * @description:
 * @author: wangyanci
 * @create: 2021-08-17 18:49
 * @Version 1.0
 **/
@Slf4j
@Controller
@RequestMapping("/work/modify")
public class WorkCompensateController extends WorkBaseController {

    @Autowired
    private OrderWorkListBService orderWorkListBService;

    @Autowired
    private CompensateBService compensateBService;
    @Autowired
    private ZsOrderWorkOperatePermissionBService orderWorkOperatePermissionBService;
    @Autowired
    private OrderPayService orderPayService;

    private static List<RefundBankVO> BANK_VO_LIST = Lists.newArrayListWithExpectedSize(3);
    static {
        BANK_VO_LIST.add(RefundBankVO.builder().account("6217003760166071662").accountBank("中国建设银行")
                .name("建设银行-1662").ocrPattern("建设.*1662|1662.*建设").build());
        BANK_VO_LIST.add(RefundBankVO.builder().account("622908343093574414").accountBank("兴业银行股份有限公司重庆自由贸易试验区分行")
                .name("兴业银行-4414").ocrPattern("兴业.*4414|4414.*兴业").build());
        BANK_VO_LIST.add(RefundBankVO.builder().account("622908343093574315").accountBank("兴业银行股份有限公司重庆自由贸易试验区分行")
                .name("兴业银行-4315").ocrPattern("兴业.*4315|4315.*兴业").build());
    }


    /**
     * 赔偿单
     */
    @RequestMapping(value = "/compensate")
    public ModelAndView compensate(Long orderId, Long workId) {
        ModelAndView mv = new ModelAndView("work/modify/compensate");
        mv.addObject("orderId", orderId);
        mv.addObject("workId", workId);
        mv.addObject("isUpdate", false);
        CompensateVO compensateVO = compensateBService.getCompensateVO(orderId, workId);
        mv.addObject("compensateVO", compensateVO);
        return mv;
    }

    /**
     * 赔偿单审核
     */
    @RequestMapping(value = "/compensate/review")
    public ModelAndView compensateReview(Long orderId, Long workId) {
        ModelAndView mv = new ModelAndView("work/modify/compensateReview");
        mv.addObject("orderId", orderId);
        mv.addObject("workId", workId);
        return mv;
    }

    /**
     * 打款凭证
     */
    @RequestMapping(value = "/remit/voucher")
    public ModelAndView remitVoucher(Long orderId, Long workId) {
        ModelAndView mv = new ModelAndView("work/modify/remitVoucher");
        mv.addObject("orderId", orderId);
        mv.addObject("workId", workId);
        RemitVoucherVO remitVoucherVO = new RemitVoucherVO();
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, workId);
        remitVoucherVO.setStatus(orderWork.getStatus());
        remitVoucherVO.setResultStatus(orderWork.getResultStatus());
        OrderPay refundPay = orderPayService.getOrderWorkRefundPay(orderId, workId);
        if (Objects.nonNull(refundPay)) {
            remitVoucherVO.setOrderPayId(refundPay.getOrderPayId());
            remitVoucherVO.setRefundVoucherData(JSONArray.parseArray(refundPay.getRefundImageData(), VoucherDataDTO.class));
            remitVoucherVO.setPayerThirdAccount(refundPay.getPayerThirdAccount());
            remitVoucherVO.setPayerThirdAccountBank(refundPay.getPayerThirdAccountBank());
        }
        mv.addObject("remitVoucherVO", remitVoucherVO);
        mv.addObject("bankVoList", BANK_VO_LIST);
        return mv;
    }

    /**
     * 赔偿单审核
     */
    @RequestMapping(value = "/review/compensate")
    @ResponseBody
    public ResultDTO reviewCompensate(@RequestBody CompensateReviewDTO compensateReviewDTO) {
        log.debug("赔偿单审核：{}", compensateReviewDTO);

        OperatePermissionVO operatePermission = orderWorkOperatePermissionBService
                .getOperatePermission(compensateReviewDTO.getOrderId(), compensateReviewDTO.getWorkId(), OrderConsts.ORDER_OP_TYPE_COMPENSATE_REVIEW);
        if (!operatePermission.getCanOperate()) {
            return ResultDTO.fail(operatePermission.getMsg());
        }

        try {
            OrderWork compensateOrderWork = compensateBService.reviewCompensate(compensateReviewDTO);
            return ResultDTO.success(compensateOrderWork);
        } catch (Exception e) {
            logger.error("compensateBService.reviewCompensate error：{}", e);
            return ResultDTO.fail(e.getMessage());
        }
    }


    /**
     * 保存赔偿单
     */
    @RequestMapping(value = "/save/compensate")
    @ResponseBody
    public ResultDTO saveCompensate(@RequestBody CompensateDTO compensateDTO) {
        log.debug("赔偿单提交：{}", compensateDTO);
        OperatePermissionVO operatePermission = orderWorkOperatePermissionBService
                .getOperatePermission(compensateDTO.getOrderId(), compensateDTO.getWorkId(), OrderConsts.ORDER_OP_TYPE_COMPENSATE);
        if (!operatePermission.getCanOperate()) {
            return ResultDTO.fail(operatePermission.getMsg());
        }
        try {
            OrderWork orderWork = compensateBService.commitCompensate(compensateDTO);
            return ResultDTO.success(orderWork);
        } catch (Exception e) {
            log.error("compensateBService.saveCompensate error:{}", e);
            return ResultDTO.fail(e.getMessage());
        }
    }

    @RequestMapping(value = "/update/compensate")
    @ResponseBody
    public ResultDTO updateCompensate(@RequestBody CompensateDTO compensateDTO) {
        log.debug("赔偿单修改：{}", compensateDTO);
        OperatePermissionVO operatePermission = orderWorkOperatePermissionBService
                .getOperatePermission(compensateDTO.getOrderId(), compensateDTO.getWorkId(), OrderConsts.ORDER_OP_TYPE_UPDATE);
        if (!operatePermission.getCanOperate()) {
            return ResultDTO.fail(operatePermission.getMsg());
        }
        try {
            OrderWork orderWork = compensateBService.updateCompensate(compensateDTO);
            return ResultDTO.success(orderWork);
        } catch (Exception e) {
            log.error("compensateBService.updateCompensate error:{}", e);
            return ResultDTO.fail(e.getMessage());
        }
    }

    /**
     * 打款凭证
     */
    @RequestMapping(value = "/save/remitVoucher")
    @ResponseBody
    public ResultDTO saveRemitVoucher(@RequestBody RemitVoucherDTO remitVoucherDTO) {
        log.debug("打款凭证提交：{}", remitVoucherDTO);
        OperatePermissionVO operatePermission = orderWorkOperatePermissionBService
                .getOperatePermission(remitVoucherDTO.getOrderId(), remitVoucherDTO.getWorkId(), OrderConsts.ORDER_OP_TYPE_REMIT_VOUCHER);
        if (!operatePermission.getCanOperate()) {
            return ResultDTO.fail(operatePermission.getMsg());
        }
        try {
            OrderWork orderWork = compensateBService.saveRemitVoucher(remitVoucherDTO);
            return ResultDTO.success(orderWork);
        } catch (Exception e) {
            log.error("compensateBService.saveRemitVoucher error：{}", e);
            return ResultDTO.fail(e.getMessage());
        }
    }


    /**
     * 赔偿单数量
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/compensateCount")
    @ResponseBody
    public ResultDTO compensateCount(Long orderId) {
        if (Objects.isNull(orderId)) {
            return ResultDTO.success(GlobalConsts.NONE);
        }
        OrderWorkQuery orderWorkQuery = new OrderWorkQuery();
        orderWorkQuery.setWorkType(Lists.newArrayList(OrderConsts.ORDER_TYPE_COMPENSATE));
        orderWorkQuery.setOrderId(orderId);
        return ResultDTO.success(orderWorkListBService.countOrderWorkByQuery(orderWorkQuery));
    }


}